/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  filename:   test.c/filemanagement
 *  copyright:  changhong electric co., ltd
 *  breif:      测试
 *  ------------------------------------------------------------------------------
 *  Rev     Date        Author          Comments
 *  1       2009-10-10  qingsong.li     Create
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "filemanagement.h"
int show_progress(float progress);
int ask_cover(char *prompt);

int main(int argc, char **argv)
{
        int i;
        PathList *list = calloc(1, sizeof(PathList));
        get_cwd_entry(list);
        for(i = 0; i < list->length; ++i)
                printf("%s\n", list->pathname[i]);
        return 0;
}

int show_progress(float progress);
int ask_cover(char *prompt);

//int main(int argc, char **argv) //TODO 有BUG，逻辑错误
//{
//        int i, target, act;
//        char pathdest[PATH_MAX];
//        PathList *list = calloc(1, sizeof(PathList));
//        PathList *tar_list = calloc(1, sizeof(PathList));
//        while (1)
//        {
//                init_list(list);
//                init_list(tar_list);
//                get_cwd_entry(list);
//                for (i = 0; i < list->length; ++i)
//                        printf("%3d: %s\n", i + 1, list->pathname[i]);
//
//                printf("请选择一个文件或文件夹：\n");
//                scanf("%d", &target);
//                getchar();
//                insert_list(tar_list, list->pathname[target - 1]);
//                if (target == 0)
//                        break;
//                printf("请选择操作种类： 0: Delete\t1: Cut\t\t2: Copy\n");
//                scanf("%d", &act);
//                getchar();
//                if (act > 0)
//                {
//                        printf("请输入要拷贝或剪切到的目的地址：\n");
//                        scanf("%s", pathdest);
//                        getchar();
//                }
//                dolist(tar_list, pathdest, act, ask_cover, show_progress);
//                destruct_list(tar_list);
//                destruct_list(list);
//        }
//
//        free(list);
//        printf("the end of the process.\n");
//        return 0;
//}

/*回调函数，0为跳过，-1为取消，1为覆盖。相当于一个对话框*/
int ask_cover(char *prompt)
{
        char ret[128];
        int type;

        while (1)
        {
                if ((type = file_type(prompt)) == TYPE_D)
                        printf(
                                        "%s已存在，(M)erge\tmer(G)eall\t(S)kip\ts(K)ipall\t(C)ancel\n",
                                        prompt);
                else
                        printf(
                                        "%s已存在，(R)eplace\tre(P)laceall\t(S)kip\ts(K)ipall\t(C)ancel\n",
                                        prompt);
                fgets(ret, 127, stdin);
                if (type == TYPE_D)
                {
                        if (strncasecmp(ret, "m", 1) == 0)
                                return 1;
                        else if (strncasecmp(ret, "g", 1) == 0)
                        {
                                COVERGFLAG |= MERGEALL;
                                return 1;
                        }
                }
                else
                {
                        if (strncasecmp(ret, "r", 1) == 0)
                                return 1;
                        else if (strncasecmp(ret, "p", 1) == 0)
                        {
                                COVERGFLAG |= REPLACEALL;
                                return 1;
                        }
                }

                if (strncasecmp(ret, "s", 1) == 0)
                        return 0;
                else if (strncasecmp(ret, "k", 1) == 0)
                {
                        COVERGFLAG |= SKIPALL;
                        return 0;
                }
                else if (strncasecmp(ret, "c", 1) == 0)
                {
                        COVERGFLAG |= CANCELALL;
                        return -1;
                }
        }
        return 1;
}

/*回调函数，用于显示复制进度*/
int show_progress(float progress)
{
        if (progress > 100.0)
                progress = 100.0;
        printf("%8.2f%%\n", progress);
        return 0;
}
